layui.use(['layer', 'form', 'element', 'table'], function() {
	var layer = layui.layer,
		form = layui.form,
		element = layui.element,
		table = layui.table;

	// 查询分类
	var tableArray = new Array();
	AjaxPostFn('system/catalog/selectallcatalog', "{}", function(jsonData) {
		$.each(jsonData.dataResultList, function(i, obj) {
				tableArray.push({
					"catalogId": obj.catalogId,
					"operation": " <a class='layui-btn ' lay-event='selectThis'  name='files'>上传主图</a><a class='layui-btn layui-bg-red ' lay-event='DelThis'>删除</a>",
					"nodeName": obj.nodeName,
					"sorted": obj.sorted,
					"classifyImage1": "<img src='" + IMGUrl + '' + obj.classifyImage + "' alt='分类主图' class='classifyimg'>",
					"recommend": obj.recommend, //是否推荐
					"layer": obj.layer //层数
				});

			})
			//展示已知数据
		var tableIns = table.render({
			elem: '#cctable',
			cols: [
				[ //标题栏
					{
						type: 'numbers',
						fixed: 'left',
						title: '行号'
					}, {
						field: 'catalogId',
						width: 100,
						title: 'Id',
						sort: true
					}, {
						field: 'operation',
						width: 250,
						title: '操作'
					}, {
						field: 'nodeName',
						width: 150,
						edit: 'text',
						title: '子类' + modDot
					}, {
						field: 'sorted',
						width: 70,
						title: '排序'
					}, {
						field: 'classifyImage1',
						width: 100,
						title: '主图'
					}, {
						field: 'recommend',
						width: 150,
						title: '是否重点分类',
						templet: '#checkboxTpl',
						unresize: true
					}

				]
			],
			data: tableArray,
			id: 'cctableid',
			size: 'lg',
			even: true,
			page: true, //是否显示分页
			done: function() {
				//图片点击放大
				TopimgFn();
			}
		});

		//是否推荐
		form.on('checkbox(lockDemo)', function(obj) {
			var recommend = 0;
			obj.elem.checked == false ? recommend = 0 : recommend = 1;
			AjaxPostFn('system/catalog/updatecatalog', "{'catalogId':'" + this.value + "','recommend':'" + recommend + "'}", function(jsonData) {
				if(jsonData.htmlState.status == 200) {
					layer.msg("修改成功");
				}
			})
		});

		//监听单元格编辑
		table.on('edit(table100)', function(obj) {
			var value = obj.value //得到修改后的值
				,
				data = obj.data //得到所在行所有键值
				,
				field = obj.field; //得到字段
			AjaxPostFn('system/catalog/updatecatalog', JSON.stringify(data), function(jsonData) {
				if(jsonData.htmlState.status == 200) {
					layer.msg("修改成功");
				}
			})
		});

		//监听表格内点击事件
		table.on('tool(table100)', function(obj) {
			var data = obj.data;
			if(obj.event === 'DelThis') {
				//删除
				layer.confirm('是否删除该条分类信息', function(index) {
					AjaxPostFn('system/catalog/deletecatalogbyId', "{'catalogId':'" + data.catalogId + "'}", function(jsonData) {
						if(jsonData.htmlState.status == 200) {
							layer.msg("删除成功");
						} else {}
					})
					obj.del();
					layer.close(index);
				});
			} else if(obj.event === 'selectThis') {
				//				上传图片
				layer.open({
					title: '上传图片',
					area: ['390px', '260px'],
					shade: 0,
					maxmin: true,
					btn: ['上传'],
					content: ' <form action="'+AllUrl+'api/qiniu/update" method="post"  enctype="multipart/form-data" target="framFile"  class="updataform"  >  <input type="file" name="files"  multiple="true" class="file_upload" />  </form>',
					yes: function() {
						thisCid = data.catalogId;
						$(".updataform").submit();
					},
					success: function(layero) {

					}
				});
				var thisCid = 1;
				var framFile = document.getElementById('framFile');
				var imgdata;
				framFile.onload = function() {
					imgdata = framFile.contentWindow.document.getElementsByTagName("pre")[0].innerHTML;
					imgdata = JSON.parse(imgdata).dataResultObj[0];
					AjaxPostFn('system/catalog/updatecatalog', "{'catalogId':'" + thisCid + "','classifyImage':'" + imgdata + "'}", function(jsonData) {
						if(jsonData.htmlState.status == 200) {
							layer.msg("修改成功");
							layer.closeAll();
							location.reload();
						}
					})
				};
				$(".upNewimg").click(function() {
					//上传图片按钮
					$(".updataform1").submit();
				})
			}
		});

	});

	//打开新弹框的事件处理
	$('.openwinbtn').on('click', function() {
		var othis = $(this),
			method = othis.data('method');
		active[method] ? active[method].call(this, othis) : '';
	});

	var active = {
		// 弹出框事件
		addmang: function() {
			layer.open({
				type: 1,
				skin: 'layui-layer-molv',
				title: '新增商品类',
				area: ['500px', '500px'],
				content: $("#addBox").html(),
				skin: 'demo-class',
				btn: ["确认新增", "关闭"],
				yes: function(index, layero) {
					if(imgdata == undefined) {
						layer.msg("请选择图片上传");
					} else {
						inputisNull("#addform input", function() {
							var thisdata = $("#addform").serializeJson();
							thisdata.classifyImage = imgdata;
							thisdata.parentsId  = '0';
							AjaxPostFn('system/catalog/insertcommodityinfo', JSON.stringify(thisdata), function(jsonData) {
								if(jsonData.htmlState.status == 200) {
									layer.msg("增加成功");
									location.reload();
								}
							})
						})
					}
				},
				success: function() {
					//下拉菜单选项
//					$("#Pidselect").append("<option value='0'>0</option>"); //处理父级类别的选择
//					form.render(); //重构form

					$(".upNewimg").val("点击上传"); //传图按钮处理
					$(".updataform1").attr("action",AllUrl+"api/qiniu/update")
					$(".updataform1").submit(); //事先先触发一次传图
					//图片上传form的submit
					$(".upNewimg").click(function() {
						
						$(".updataform1").submit();
					})

					//图片上传的frame的窗口
					var framFile1 = document.getElementById('framFile1');
					framFile1.onload = function() {
						//action触发iframe
						console.log("最新窗口点击");
						imgdata = framFile1.contentWindow.document.getElementsByTagName("pre")[0].innerHTML;
						imgdata = JSON.parse(imgdata).dataResultObj[0];
						console.log(imgdata);
						if(imgdata == null) {
							$(".upNewimg").val("点击上传");
						} else {
							$(".upNewimg").val("上传成功");
						}

					}
				}
			})
		}
	}

});